/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cinema;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
 *
 * @author Lucas
 */
public class Main extends javax.swing.JFrame {

    public static ArrayList<Sessao> lsessao = new ArrayList<>();
    public static ArrayList<Sala> lsala = new ArrayList<>();

    public static void addsala(Sala adicionar) {
        lsala.add(adicionar);
    }

    public static void removesala(Sala adicionar) {
        lsala.remove(adicionar);
    }

    public static void addsessao(Sessao adicionar) {
        lsessao.add(adicionar);
    }

    public static void removesessao(Sessao adicionar) {
        lsessao.remove(adicionar);
    }

    private void iniFilmes() {
        Sala sa = new Sala();
        Sessao se = new Sessao();

        sa.setId(1);
        sa.setCapacidade(150);
        addsala(sa);

        se.setFilme("The Lord of the Rings - The Fellowship of the Ring");
        se.setData("28/07/2013");
        se.setHora("13:00");
        se.setSala(sa);
        se.setLotacao(sa.getCapacidade());
        addsessao(se);



        sa = new Sala();
        se = new Sessao();

        sa.setId(2);
        sa.setCapacidade(150);
        addsala(sa);

        se.setFilme("Batman - The Dark Knight Rises");
        se.setData("28/07/2013");
        se.setHora("13:30");
        se.setSala(sa);
        se.setLotacao(sa.getCapacidade());
        addsessao(se);



        sa = new Sala();
        se = new Sessao();

        sa.setId(3);
        sa.setCapacidade(150);
        addsala(sa);

        se.setFilme("The Goodfather");
        se.setData("28/07/2013");
        se.setHora("14:00");
        se.setSala(sa);
        se.setLotacao(sa.getCapacidade());
        addsessao(se);

        se = new Sessao();

        se.setFilme("A Clockwork Orange");
        se.setData("28/07/2013");
        se.setHora("16:30");
        se.setSala(sa);
        se.setLotacao(sa.getCapacidade());
        addsessao(se);



        sa = new Sala();
        se = new Sessao();

        sa.setId(4);
        sa.setCapacidade(150);
        addsala(sa);

        se.setFilme("Mad Max Beyond Thunderdome");
        se.setData("28/07/2013");
        se.setHora("14:00");
        se.setSala(sa);
        se.setLotacao(sa.getCapacidade());
        addsessao(se);

    }

    public void createCsvFile() {
        try {
            BufferedWriter StrW = new BufferedWriter(new FileWriter("C:/Documents and Settings/Lucas/Meus documentos/NetBeansProjects/Cinema/Cinema/src/Cinema/relatorio.txt"));
            if (lsessao.isEmpty()) {
                StrW.write("Relatório vazio");
            } else {
                for (Sessao s : lsessao) {
                    StrW.write("Filme: " + s.getFilme());
                    StrW.newLine();
                    StrW.write("Hora: " + s.getHora());
                    StrW.newLine();
                    StrW.write("Data: " + s.getData());
                    StrW.newLine();
                    StrW.write("Sala: " + s.getSala().getId());
                    StrW.newLine();
                    StrW.write("Lugares vagos: " + s.getLotacao());
                    StrW.newLine();
                    StrW.newLine();
                    StrW.newLine();
                }
            }
            StrW.close();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Creates new form Main
     */
    public Main() {
        initComponents();
        iniFilmes();
        new minhaThread(jLabel1).start();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton3 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Bilheteria");

        jButton3.setText("Venda");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Gerar Relatório");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jMenu1.setText("Sala");

        jMenuItem1.setText("Cadastrar Sala");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Excluir Sala");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Sessão");

        jMenuItem3.setText("Cadastrar Sessão");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem3);

        jMenuItem4.setText("Excluir Sessão");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem4);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(130, 130, 130)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 70, Short.MAX_VALUE)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(50, 50, 50))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton4)
                    .addComponent(jButton3))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        Cadastrosala formcadastrosala = new Cadastrosala();
        formcadastrosala.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        Excluirsala formExcluirsala = new Excluirsala();
        formExcluirsala.setVisible(true);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        Cadastrosessao formcadastrosessao = new Cadastrosessao();
        formcadastrosessao.setVisible(true);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        Excluirsessao formExcluirsessao = new Excluirsessao();
        formExcluirsessao.setVisible(true);
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        Venda formVenda = new Venda();
        formVenda.setVisible(true);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        try {
            createCsvFile();
            JOptionPane.showMessageDialog(rootPane, "Relatório gerado com sucesso.");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, "Erro ao gerar relatório.");
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Main().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    // End of variables declaration//GEN-END:variables
}
